<template>
    <div class="list">
        <!-- 面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">{{
                $t('goods.home')
            }}</el-breadcrumb-item>
            <el-breadcrumb-item>{{
                $t('goods.goodsManage')
            }}</el-breadcrumb-item>
            <el-breadcrumb-item>{{ $t('goods.goodsList') }}</el-breadcrumb-item>
        </el-breadcrumb>

        <!-- 搜索和添加商品 -->
        <el-row class="tools">
            <el-col :span="6">
                <!-- 搜索商品 -->
                <el-input
                    :placeholder="$t('goods.goodsPlaceholder')"
                    class="input-with-select"
                    clearable
                    v-model="queryInfo.query"
                    v-on:clear="getGoodsList"
                >
                    <el-button
                        slot="append"
                        icon="el-icon-search"
                        v-on:click="filterGoods"
                    ></el-button>
                </el-input>
            </el-col>
            <el-col :span="3" class="center">
                <!-- 添加用户 -->
                <el-button type="primary" v-on:click="goodsAddpage">{{
                    $t('goods.add')
                }}</el-button>
            </el-col>
        </el-row>

        <!-- 商品列表 -->
        <el-card>
            <!-- 展示的商品列表数据信息 -->
            <el-table :data="goodsList.goods" border style="width: 100%">
                <el-table-column
                    type="index"
                    :label="$t('goods.id')"
                    width="60"
                    align="center"
                ></el-table-column>
                <el-table-column
                    prop="goods_name"
                    :label="$t('goods.goodsName')"
                    width="350"
                >
                </el-table-column>
                <el-table-column
                    prop="goods_price"
                    :label="$t('goods.goodsPrice')"
                    width="150"
                >
                </el-table-column>
                <el-table-column
                    prop="goods_weight"
                    :label="$t('goods.goodsWeight')"
                    width="150"
                >
                </el-table-column>
                <el-table-column
                    prop="add_time"
                    :label="$t('goods.createTime')"
                    width="250"
                >
                    <template slot-scope="scope">
                        {{ (scope.row.add_time * 1000) | dateFormat }}
                    </template>
                </el-table-column>
                <el-table-column prop="goods_state" :label="$t('goods.status')">
                    <template slot-scope="scope">
                        <el-tag type="danger" v-if="scope.row.goods_state === 0"
                            >未通过</el-tag
                        >
                        <el-tag v-else-if="scope.row.goods_state === 1"
                            >审核中</el-tag
                        >
                        <el-tag type="success" v-else>已审核</el-tag>
                    </template>
                </el-table-column>
                <!-- 操作按钮 -->
                <el-table-column :label="$t('goods.operate')" width="150">
                    <template slot-scope="scope">
                        <el-tooltip
                            class="item"
                            effect="dark"
                            :content="$t('goods.editGoods')"
                            placement="top-end"
                        >
                            <el-button
                                size="mini"
                                type="primary"
                                icon="el-icon-edit"
                                circle
                                v-on:click="eidteRouter(scope.row)"
                            ></el-button>
                        </el-tooltip>

                        <el-tooltip
                            class="item"
                            effect="dark"
                            :content="$t('goods.delGoods')"
                            placement="top-end"
                        >
                            <el-button
                                size="mini"
                                type="danger"
                                icon="el-icon-delete"
                                circle
                                v-on:click="delGoods(scope.row)"
                            ></el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>
            </el-table>

            <!-- 分页 -->
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum"
                :page-size="queryInfo.pagesize"
                :page-sizes="[1, 3, 5, 10]"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
            >
            </el-pagination>
        </el-card>
    </div>
</template>

<script>
export default {
    name: 'goodsList',
    data() {
        return {
            // 查询参数对象
            queryInfo: {
                // 要搜索的关键字
                query: '',
                // 当前页码
                pagenum: 1,
                // 每页显示数量
                pagesize: 3,
            },
            // 商品列表数据
            goodsList: [],
            // 总记录数
            total: 0,
        }
    },
    methods: {
        // 获取商品列表
        async getGoodsList() {
            // 发送请求
            const { data: res } = await this.$http.get('/goods', {
                headers: {
                    Authorization: localStorage.getItem('token'),
                },
                params: this.queryInfo,
            })
            // 获取 msg 和 status
            const {
                meta: { msg, status },
            } = res
            // 如果请求失败
            if (status !== 200) {
                // 提示消息
                return this.$message.error(msg)
            }
            // 为商品列表数据赋值
            this.goodsList = res.data
            // 为总记录数赋值
            this.total = res.data.total
        },
        // 监听 pagesize 改变的方法
        handleSizeChange(newSize) {
            this.queryInfo.pagesize = newSize
            // 获取商品列表
            this.getGoodsList()
        },
        // 监听 pagenum 改变的方法
        handleCurrentChange(newNum) {
            this.queryInfo.pagenum = newNum
            // 获取商品列表
            this.getGoodsList()
        },
        // 添加商品
        goodsAddpage() {
            this.$router.push('/goods/add')
        },
        // 删除商品
        async delGoods(row) {
            console.log(row.goods_id)
            try {
                // 弹出对话框
                const result = await this.$confirm(
                    '此操作将永久删除该商品, 是否继续?',
                    '提示',
                    {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning',
                    }
                )
                /*
                    - 如果用户点击确定，result 为 confirm
                    - 如果用户点击取消，result 为 cancel
                */
                if (result === 'confirm') {
                    // 发送请求
                    const { data: res } = await this.$http.delete(
                        `goods/${row.goods_id}`,
                        {
                            headers: {
                                Authorization: localStorage.getItem('token'),
                            },
                        }
                    )
                    // 获取 msg 和 status
                    const {
                        meta: { msg, status },
                    } = res
                    // 如果删除成功
                    if (status === 200) {
                        this.$message({
                            type: 'success',
                            message: msg,
                        })
                        // 获取商品列表
                        this.getGoodsList()
                        return
                    }
                    // 删除失败时
                    this.$message({
                        type: 'error',
                        message: msg,
                    })
                }
            } catch (err) {
                console.log(err)
                // 若点击取消删除按钮时
                if (err === 'cancel') {
                    this.$message({
                        type: 'info',
                        message: '已取消删除',
                    })
                }
            }
        },
        // 编辑商品
        eidteRouter(row) {
            // 将当前编辑商品的信息存到本地存储中
            localStorage.setItem('editeGoodsID', JSON.stringify(row.goods_id))
            this.$router.push('/goods/edite')
        },
        // 筛选搜索之后的商品内容
        filterGoods() {
            if (this.queryInfo.query !== '') {
                this.goodsList.goods = this.goodsList.goods.filter((item) => {
                    return item.goods_name.includes(this.queryInfo.query)
                })
            }
        },
    },
    created() {
        // 获取商品列表
        this.getGoodsList()
    },
}
</script>

<style lang="less" scoped>
// 将添加商品按钮居中
.center {
    text-align: center;
}
</style>
